package reflect;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : MethodTest
 * @Author : 54061
 * @Date: 2022/2/28 17:59
 */
/* 获取Class对象所对应类型中所有公共的方法，包括继承父类的
        * Method[] getMethods();
        * 获取Class对象所对应类型中所有的方法，不包括继承父类的
        * Method[] getDeclaredMethods();
        */
public class MethodTest {

    @Test
    public void testGetMethod() throws Exception {
        Class<Student> clazz = Student.class;
        Constructor<Student> constructor = clazz.getConstructor();
        Student student = constructor.newInstance();
        Method setNameMethod = clazz.getMethod("setName", String.class);
        setNameMethod.invoke(student, "王五");
        System.out.println(student);
        Method getNameMethod = clazz.getMethod("getName");
        Object o = getNameMethod.invoke(student);
        System.out.println(o);
    }

    @Test
    public void testGetMethods(){
        Class<Student> clazz = Student.class;
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("+++++++++++++++++++++++++++++");
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
    }

}